<?php

namespace app\api\controller;



use app\BaseController;
use app\service\AlipayDecrypt;
use app\service\apixcxService;
use think\facade\Db;
use util\JwtUtils;

class User extends BaseController
{
    public function login(){
        $code=request()->param("auth_code");
        $api = new apixcxService();
        //获取当前用户支付宝ID
        $oauthtoken = $api->xcxoauthToken($code);
        if(property_exists($oauthtoken,'sub_msg')){
            parent::sendOutput($oauthtoken->sub_msg,500);
            //return json(['msg' => $oauthtoken->sub_msg,'code'=>"500"]);
        }

        $user_id = $oauthtoken->open_id;
        $info=Db::table("room_user")
            ->where("user_id",$user_id)
            ->find();
        $arr["userid"]=$user_id;
        $token=JwtUtils::getToken($arr);

        $data["accessToken"]=$token;
        if(empty($info)){
            $data["bindRoomStatus"]=false;
            parent::sendOutput($data);
        }
        $room=Db::table("roominfo")->where("id",$info["room_id"])->find();
        $data["roominfo"]=$room;
        parent::sendOutput($data);
    }
    //用户绑定房屋
    public function bindRoom(){
        $code=request()->param("auth_code");
        $api = new apixcxService();
        //获取当前用户支付宝ID
        $oauthtoken = $api->xcxoauthToken($code);
        if(property_exists($oauthtoken,'sub_msg')){
            parent::sendOutput($oauthtoken->sub_msg,500);
            //return json(['msg' => $oauthtoken->sub_msg,'code'=>"500"]);
        }
        $user_id = $oauthtoken->open_id;//$oauthtoken->user_id;
        $str=request()->param("str");
        $result=openssl_decrypt(base64_decode($str), 'AES-128-CBC', base64_decode(env("APP.AES_KEEY")),OPENSSL_RAW_DATA);
        $result= json_decode($result, true);
        if($result["code"]!="10000"){
            parent::sendOutput("用户手机号获取失败",500);
        }
        $nick_name=request()->param("nickName");
        $avatar=request()->param("image");
        $mobile=$result["mobile"];
        $roominfo=Db::table("roominfo")->where("owner_phone",$mobile)->find();
        if(empty($roominfo)){
            parent::sendOutput("房屋信息不存在",500);
        }
        $user_room=Db::table("room_user")
            ->where("user_id",$user_id)
            ->where("room_id",$roominfo["id"])
            ->find();
        if(empty($user_room)){
            $community=Db::table("community")->where("community_id",$roominfo["community_id"])->find();
            $add["user_id"]=$user_id;
            $add["room_id"]=$roominfo["id"];
            $add["community_id"]=$roominfo["community_id"];
            $add["community_name"]=$community["community_name"];
            $add["type"]=1;
            $add["create_time"]=date("Y-m-d H:i;s");
            $add["mobile"]=$mobile;
            $add["nick_name"]=$nick_name;
            $add["avatar"]= $avatar;
            $add["name"]=$roominfo["owner_name"];
            $add["address"]=$roominfo["address"];
            Db::table("room_user")->insert($add);

            $arr["userid"]=$user_id;
            $token=JwtUtils::getToken($arr);
            $data["accessToken"]=$token;
            $data["bindRoomStatus"]=true;
            $data["alreadyBind"]=false;
            $data["tipsMsg"]="授权成功";
            $data["roominfo"]=$roominfo;
            parent::sendOutput($data);
        }else{
//            if($user_room["type"]=="1"){
//                parent::sendOutput("该手机号已绑定家人",500);
//            }
            $add["nick_name"]=$nick_name;
            $add["avatar"]= $avatar;
            $add["update_time"]=date("Y-m-d H:i;s");
            Db::table("room_user")->where("id",$user_room["id"])->update($add);
            $arr["userid"]=$user_id;
            $token=JwtUtils::getToken($arr);
            $data["accessToken"]=$token;
            $data["bindRoomStatus"]=true;
            $data["roominfo"]=$roominfo;
            $data["alreadyBind"]=true;
            $data["tipsMsg"]="你已经绑定房屋";
            parent::sendOutput($data);

        }

    }
    public function familyBind(){
        $code=request()->param("auth_code");
        $api = new apixcxService();
        //获取当前用户支付宝ID
        $oauthtoken = $api->xcxoauthToken($code);
        if(property_exists($oauthtoken,'sub_msg')){
            parent::sendOutput($oauthtoken->sub_msg,500);
            //return json(['msg' => $oauthtoken->sub_msg,'code'=>"500"]);
        }
        $user_id = $oauthtoken->user_id;
        $str=request()->param("str");
        $result=openssl_decrypt(base64_decode($str), 'AES-128-CBC', base64_decode(env("APP.AES_KEEY")),OPENSSL_RAW_DATA);
        $result= json_decode($result, true);
        if($result["code"]!="10000"){
            parent::sendOutput("用户手机号获取失败",500);
        }
        $mobile=$result["mobile"];
        $user_room=Db::table("room_user")
            ->where("type",2)
            ->where("mobile", $mobile)
            ->find();
        if(!empty($user_room)){
            if(!empty($user_room["user_id"])){
                parent::sendOutput("该手机已经绑定了",500);
            }
            $community=Db::table("community")->where("community_id",$user_room["community_id"])->find();
            $roominfo=Db::table("roominfo")->where("id",$user_room["room_id"])->find();
            if(empty($roominfo)){
                parent::sendOutput("房屋信息不存在",500);
            }
            $add["user_id"]=$user_id;
            $add["room_id"]=$roominfo["id"];
            $add["community_id"]=$roominfo["community_id"];
            $add["community_name"]=$community["community_name"];
            $add["type"]=1;
            $add["create_time"]=date("Y-m-d H:i;s");
            $add["mobile"]=$mobile;
            $add["name"]=$roominfo["owner_name"];
            $add["address"]=$roominfo["address"];
            Db::table("room_user")->insert($add);

            $arr["userid"]=$user_id;
            $token=JwtUtils::getToken($arr);
            $data["accessToken"]=$token;
            $data["bindRoomStatus"]=true;

            $data["roominfo"]=$roominfo;
            parent::sendOutput($data);
        }else{
            parent::sendOutput("该手机号码不存在，请联系管理员");

        }

    }

    public function visitorVerify(){
        $code=request()->param("auth_code");
        $api = new apixcxService();
        //获取当前用户支付宝ID
        $oauthtoken = $api->xcxoauthToken($code);
        if(property_exists($oauthtoken,'sub_msg')){
            parent::sendOutput($oauthtoken->sub_msg,500);
            //return json(['msg' => $oauthtoken->sub_msg,'code'=>"500"]);
        }
        $user_id = $oauthtoken->user_id;
        $str=request()->param("str");
        $result=openssl_decrypt(base64_decode($str), 'AES-128-CBC', base64_decode(env("APP.AES_KEEY")),OPENSSL_RAW_DATA);
        $result= json_decode($result, true);
        if($result["code"]!="10000"){
            parent::sendOutput("用户手机号获取失败",500);
        }
        $mobile=$result["mobile"];
        $visitor=Db::table("visitor")
            ->where("mobile", $mobile)
            ->find();
        if(!empty($visitor)){
            if(!empty($visitor["user_id"])){
                parent::sendOutput("该手机已经绑定了",500);
            }
            $community=Db::table("community")->where("community_id",$visitor["community_id"])->find();
            $roominfo=Db::table("roominfo")->where("id",$visitor["room_id"])->find();
            if(empty($roominfo)){
                parent::sendOutput("房屋信息不存在",500);
            }

            Db::table("visitor")->where("id",$visitor["id"])->update(["user_id"=>$user_id]);

            $arr["userid"]=$user_id;
            $token=JwtUtils::getToken($arr);
            $data["accessToken"]=$token;
            $data["bindRoomStatus"]=true;

            $data["roominfo"]=$roominfo;
            parent::sendOutput($data);
        }else{
            parent::sendOutput("该手机号码不存在，请联系管理员",500);

        }

    }
}